package com.rf.richfitwheel.admin.sys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.dao.SysCityInfoMapper;
import com.rf.richfitwheel.admin.sys.model.SysCityInfo;
import com.rf.richfitwheel.admin.sys.model.SysCityInfoExample;
import com.rf.richfitwheel.admin.sys.service.SysCityInfoService;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.admin.utils.UserUtil;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Jia XiaoLong
 * @className SysCityInfoServiceImpl
 * @Date 2022/1/17
 * @Description
 */
@Service("sysCityInfoService")
public class SysCityInfoServiceImpl implements SysCityInfoService {

        @Autowired
        private SysCityInfoMapper sysCityInfoMapper;

        @Override
        public PageUtils queryPage(Map<String, Object> params) {
            PageUtils pageUtils = PageUtils.getPageInfo(params);
            Page<SysCityInfo> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
            SysCityInfo sysCityInfo = (SysCityInfo) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")), SysCityInfo.class);
            SysCityInfoExample example = new SysCityInfoExample();
            SysCityInfoExample.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotBlank(sysCityInfo.getLoclannm())){
                criteria.andLoclannmLike("%"+sysCityInfo.getLoclannm()+"%");
            }
            if(StringUtils.isNotBlank(sysCityInfo.getCtyco())){
                criteria.andCtycoLike("%" + sysCityInfo.getCtyco() + "%");
            }
            example.setOrderByClause(" ctyco ");
            List<SysCityInfo> list = sysCityInfoMapper.selectByExample(example);
            pageUtils = new PageUtils(p);
            return pageUtils;
        }

        @Override
        public void save(SysCityInfo sysCityInfo) throws Exception{
            SysCityInfoExample example = new SysCityInfoExample();
            SysCityInfoExample.Criteria criteria = example.createCriteria();
            criteria.andCtycoEqualTo(sysCityInfo.getCtyco());
            List<SysCityInfo> list = sysCityInfoMapper.selectByExample(example);
            if(!list.isEmpty()){
                throw new BusinessException("国家代码不允许重复");
            }
            UserVO userVO = UserUtil.getUser();
            sysCityInfo.setId(UuidUtil.get32UUID());
            sysCityInfo.setCreateUser(userVO.getUsername());
            sysCityInfo.setCreateTime(new Date());
            sysCityInfoMapper.insert(sysCityInfo);
        }

        @Override
        public void update(SysCityInfo sysCityInfo) throws Exception{
            SysCityInfoExample example = new SysCityInfoExample();
            SysCityInfoExample.Criteria criteria = example.createCriteria();
            criteria.andCtycoEqualTo(sysCityInfo.getCtyco());
            criteria.andIdNotEqualTo(sysCityInfo.getId());
            List<SysCityInfo> list = sysCityInfoMapper.selectByExample(example);
            if(!list.isEmpty()){
                throw new BusinessException("国家代码不允许重复");
            }
            UserVO userVO = UserUtil.getUser();
            sysCityInfo.setModTime(new Date());
            sysCityInfo.setModUser(userVO.getUsername());
            sysCityInfoMapper.updateByPrimaryKey(sysCityInfo);
        }

        @Override
        public void deleteBatch(String[] ids) {
            for (String id : ids) {
                sysCityInfoMapper.deleteByPrimaryKey(id);
            }
        }

        @Override
        public SysCityInfo selectById(String id) {
            return sysCityInfoMapper.selectByPrimaryKey(id);
        }

        @Override
        public List<SysCityInfo> getCityList(Map<String, Object> params) {
        SysCityInfoExample example = new SysCityInfoExample();
        SysCityInfoExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause("create_time asc");
        List<SysCityInfo> list = sysCityInfoMapper.selectByExample(example);
        return list;
    }

}
